package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MenuDao;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MenuServiceImpl implements MenuService {


    @Autowired
    private MenuDao menuDao;

    public List<Map<String, Object>> getMenu(Integer id) {

        List<Menu> menuList = menuDao.findMenu(id);
        List<Map<String,Object>> list = new ArrayList<>();

        for (Menu menu : menuList) {
            Map<String,Object> map = null;
            List<Map> children = null;
            if (menu.getParentMenuId() == null){
                map = new HashMap<>();
                map.put("path",menu.getPath());//1
                map.put("title",menu.getName());
                map.put("icon",menu.getIcon());
                List<Menu> menuList1 = menuDao.findTwoMenu(menu.getId());
                children = new ArrayList<>();
                for (Menu menu1 : menuList1) {
                    Map<String,Object> map1 = new HashMap<>();
                    map1.put("path",menu1.getPath());
                    map1.put("title",menu1.getName());
                    map1.put("linkUrl",menu1.getLinkUrl());
                    children.add(map1);
                }
                map.put("children",children);
                list.add(map);
            }


        }

       /* for (Menu menu : menuList) {
            Map<String,Object> map = new HashMap<>();
            map.put("path",menu.getPath());
            map.put("title",menu.getName());
            map.put("icon",menu.getIcon());
            List<Menu> menuList1 = menuDao.findTwoMenu(menu.getId());
            List<Map> children = new ArrayList<>();
            for (Menu menu1 : menuList1) {
                Map<String,Object> map1 = new HashMap<>();
                map1.put("path",menu1.getPath());
                map1.put("title",menu1.getName());
                map1.put("linkUrl",menu1.getLinkUrl());
                children.add(map1);
            }
            map.put("children",children);
            list.add(map);
        }*/
        return list;
    }
}
